<?php

class MessageManager extends Base {

	private static $messages = null;
	private static $config = null;
	private static $langDefault = null;

	public static function build() {
		$arr = MessageManager::getAllMessages();

		$infoMessage = "";
		$successMessage = "";
		$warningMessage = "";
		$errorMessage = "";
			
		if( $infoMessage != null )
			echo "<div id='message-manager' class='info'>$infoMessage</div>";
		if( $successMessage != null )
			echo "<div id='message-manager' class='success'>$successMessage</div>";
		if( $warningMessage != null )
			echo "<div id='message-manager' class='warning'>$warningMessage</div>";
		if( $errorMessage != null )
			echo "<div id='message-manager' class='error'>$errorMessage</div>";
	}

	public static function getMessages($messageType) {
		Logger::debug("MessageManager","Recuperando mensagens do gestor de mensagens para o tipo '$messageType'");
			
		$messages_manager = Scope::loadRequestVar( Constants::USER_MESSAGE );
		$arr = array();
			
		if( $messages_manager != null ) {
			foreach( $messages_manager as $action ) {
				if( $action->getMessageType() == $messageType ) {
					$arr[] = $action;
				}
			}
		}
			
		return $arr;
	}

	public static function getAllMessages() {
		$arr = array();
		$arr = array_merge($arr, MessageManager::getMessages(Constants::MESSAGE_TYPE_INFO));
		$arr = array_merge($arr, MessageManager::getMessages(Constants::MESSAGE_TYPE_SUCCESS));
		$arr = array_merge($arr, MessageManager::getMessages(Constants::MESSAGE_TYPE_WARNING));
		$arr = array_merge($arr, MessageManager::getMessages(Constants::MESSAGE_TYPE_ERROR));
		return $arr;
	}

	public static function addMessage($message, $messageType) {
		Logger::debug("MessageManager","Adicionando a mensagem de erro '$message' no gestor de mensagens");
			
		$messages_manager = Scope::loadRequestVar( Constants::USER_MESSAGE );
			
		if( $messages_manager == null ) {
			$messages_manager = array();
		}
			
		$actionMessage = new ActionMessage();
		$actionMessage->setMessage($message);
		$actionMessage->setMessageType($messageType);
			
		$messages_manager[] = $actionMessage;
			
		Scope::putRequestVar( Constants::USER_MESSAGE, $messages_manager );
	}

	public static function addInfoMessage($message) {
		self::addMessage($message, Constants::MESSAGE_TYPE_INFO);
	}

	public static function addSuccessMessage($message) {
		self::addMessage($message, Constants::MESSAGE_TYPE_SUCCESS);
	}

	public static function addWarningMessage($message) {
		self::addMessage($message, Constants::MESSAGE_TYPE_WARNING);
	}

	public static function addErrorMessage($message) {
		self::addMessage($message, Constants::MESSAGE_TYPE_ERROR);
	}

	public static function getString($key) {
		Logger::debug("MessageManager", "Recuperando a mensagem identificada pela key '$key'");
		$temp = self::loadMessages();
		$value = $temp[$key];

		if( $value == null || $value == "" ) {
			return $key;
		} else {
			return $value;
		}
	}

	public static function getConfig($key) {
		$arr = Config::getConfig("configuration");
		if( isset($arr[$key]) ) {
			return $arr[$key];
		} else {
			die("A key '$key' nao foi encontrada no arquivo de configuracao");
		}
	}

	private static function loadMessages() {
		Logger::debug("MessageManager", "Carregando o arquivo de mensagens");
		if( self::$langDefault == null ) {
			Logger::debug("Messages", "Recuperando o locale da sessao do usuario");
			self::$langDefault = Scope::getLocale();
		}
			
		$lang = Scope::getLocale();
		Logger::debug("Messages", "Locale = $lang");
			
		if( self::$langDefault != $lang || self::$messages == null ) {
			$path = "classes/config/messages_" . $lang . ".txt";
			self::$messages = FileUtils::loadPropertyInArray($path);
		}
		return self::$messages;
	}

	public static function hasMessages() {
		return sizeof(self::getAllMessages()) > 0;
	}
}
?>